﻿var keyValue = request('keyValue');

var companyId = request('companyId');
var departmentId = request('departmentId');

var personKeyword = "";
var personSpellChar = "";


var index = request("index");
var result = {};

var removeSelectPerson;
var addSelectPerson;

var acceptClick;
var bootstrap = function ($, learun) {
    "use strict";


    //判断选择出行人员是否重复
    function isRepeatPerson(id) {
        var res = false;
        for (var i = 0, l = result.persons.length; i < l; i++) {
            if (result.persons[i].id == id) {
                res = true;
                break;
            }
        }
        return res;
    }

    //添加出行人员
    addSelectPerson = function (id) {
        var userName = $(id).html();
        var userId = $(id).attr("data-id");
        var officialBankAccount = $(id).attr("data-officialBankAccount");

        if(!isRepeatPerson(userId)) {
            var user = {id:userId, realName:userName,officialBankAccount:officialBankAccount}
            result.persons.push(user);

            var $html =  '<div class="oppbox" style="margin-right:10px;" data-id="'+userId+'"> ';
                $html+=  '   <span class="oppbox-text black" style="color:#000;">'+userName+'</span>';
                $html+=  '   <div class="oppbox oppbox-item-del" onclick="removeSelectPerson(this)">&nbsp;</div>';
                $html+=  '</div>';
            $("#pnlTravelPerson").append($html);
        }
    }

    //删除出行人员
    removeSelectPerson = function (id) {
        var userId = $(id).parent().attr("data-id");
        for (var i = 0, l = result.persons.length; i < l; i++) {
            if (result.persons[i].id == userId) {
                result.persons.splice(i, 1);
                break;
            }
        }
        $(id).parent().remove();
    }

    //检索出行人员
    function searchPerson(keyword, simpleSpelling) {
        var paramStr =  "";
        if(!!companyId) {
            paramStr += "&companyId=" + companyId;
        }
        if(!!departmentId) {
            paramStr += "&departmentId=" + departmentId;
        }
        if(!!keyword) {
            paramStr += "&keyword=" + keyword;
        }
        if(!!simpleSpelling) {
            paramStr += "&simpleSpelling=" + simpleSpelling;
        }
        learun.httpAsyncGet2(config.base_server() + 'users/list', paramStr, function (res) {
            learun.loading(false);

            if (res.resp_code == 200) {
                $("#pnlTravelSelectPersonResult").empty();
                var $html = "";
                for (var i = 0, l = res.data.length; i < l; i++) {
                    if(!res.data[i].officialBankAccount){
                        res.data[i].officialBankAccount = "";
                    }
                    $html += '<div class="oppbox oppbox-gray" style="margin-right:10px;"><a href="javascript:void(0);" onclick="addSelectPerson(this)" data-id="'+res.data[i].id+'" data-officialBankAccount="'+res.data[i].officialBankAccount+'" class="oppbox-text">'+res.data[i].realName+'</a></div>';
                }
                if($html=='') {
                    $html = '<span class="nk-receipt-label b i" n="查找结果" style="width: auto; color: rgb(238, 17, 17); display: inline-block;">查找不到对应的人员，请重新查找...</span>';
                }
                $("#pnlTravelSelectPersonResult").append($html);
            }
        });
    }

    //绘制出行人员
    function redrawPerson() {
       $("#pnlTravelPerson").empty();
       var $html = "";
       for (var i = 0, l = result.persons.length; i < l; i++) {
           $html +=  '<div class="oppbox" style="margin-right:10px;" data-id="'+result.persons[i].id+'"> ';
           $html +=  '   <span class="oppbox-text black" style="color:#000;">'+result.persons[i].realName+'</span>';
           $html +=  '   <div class="oppbox oppbox-item-del" onclick="removeSelectPerson(this)">&nbsp;</div>';
           $html +=  '</div>';
       }
       $("#pnlTravelPerson").append($html);
    }

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        /*绑定事件和初始化控件*/
        bind: function () {
            // 查询出行人员列表
            $('#txtTravelSelectPersonFindName').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    searchPerson(keyword, null);
                }
            });
            $('#btnTravelSelectPersonFind').on("click", function (e) {
                var keyword = $("#txtTravelSelectPersonFindName").val();
                searchPerson(keyword, null);
            });
            $('#pnlTravelSelectPersonChar >a').on("click", function (e) {
                var $this = $(this);
                var splling = $this.html();
                searchPerson(null, splling);
            });
        },
        /*初始化数据*/
        initData: function () {
            if(!!index) {
               result = $(config.getParent(learun).document).find("#wfFormIframe0")[0].contentWindow.routes[index];
               redrawPerson();
            } else {
                result.persons = [];
            }
        }
    };

    // 保存数据
    acceptClick = function (callBack) {
        if (!!callBack) {
            callBack(result.persons, index);
        }
        return true;
    };

    page.init();
}